package com.example.vv.model.home.base.adapter;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class ChatDetailItemDecoration extends RecyclerView.ItemDecoration {
    private int space;
    private int top;

    public ChatDetailItemDecoration(int space, int top) {
        this.space = space;
        this.top = top;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int childAdapterPosition = parent.getChildAdapterPosition(view);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            int spanCount = gridLayoutManager.getSpanCount();
            if (childAdapterPosition==0||spanCount%childAdapterPosition==0) {
                outRect.set(space/2,top,space/2,0);
            }else if (spanCount%(childAdapterPosition+1)==0){
                outRect.set(space/2,top,space,0);
            }else {
                outRect.set(space/2,top,space/2,0);
            }
        }
    }
}
